package com.sky.service.impl;

import com.sky.context.BaseContext;
import com.sky.dto.ShoppingCartDTO;
import com.sky.entity.ShoppingCart;
import com.sky.exception.ShoppingCartBusinessException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.ShoppingCartMapper;
import com.sky.service.ShoppingCartService;
import com.sky.vo.DishVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;


@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {

    @Autowired
    private ShoppingCartMapper  shoppingCartMapper;

    @Autowired
    private DishMapper dishMapper;

    @Autowired
    private SetmealMapper setmealMapper;

    /**
     * 添加购物车
     *
     * @param shoppingCartDTO
     */
    public void addShopingCarts(ShoppingCartDTO shoppingCartDTO) {
        // 完善购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO, shoppingCart);
        // 仅能查询自己的购物车数据
        shoppingCart.setUserId(BaseContext.getCurrentId());

        // 如果已有该商品或套餐信息，则数量+1
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);

        if( list != null && list.size() > 0 ){
            shoppingCart = list.get(0);
            shoppingCart.setNumber(shoppingCart.getNumber()+1);
            shoppingCartMapper.editOneById(shoppingCart);
        }else{ //否则插入数据
            Long dishId = shoppingCart.getDishId();

            if (dishId != null) { // 是菜品

                DishVO dish = dishMapper.getDishById(dishId);
                shoppingCart.setImage(dish.getImage());
                shoppingCart.setName(dish.getName());
                shoppingCart.setAmount(dish.getPrice());

            }else { // 是套餐

                SetmealVO setmeal = setmealMapper.getSetmealById(Math.toIntExact(shoppingCart.getSetmealId()));
                shoppingCart.setImage(setmeal.getImage());
                shoppingCart.setName(setmeal.getName());
                shoppingCart.setAmount(setmeal.getPrice());
            }

            shoppingCart.setCreateTime(LocalDateTime.now());
            shoppingCart.setNumber(1);
            List<ShoppingCart> shoppingCartList = Arrays.asList(shoppingCart);
            shoppingCartMapper.insertBatch(shoppingCartList);
        }
    }

    /**
     * 清空购物车
     */
    public void cleanShoppingCart() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        shoppingCartMapper.delShoppingCart(shoppingCart);
    }

    /**
     * 删除购物车
     *
     * @param shoppingCartDTO
     */
    public void subShoppingCart(ShoppingCartDTO shoppingCartDTO) {
        // 获取购物车对象
        ShoppingCart shoppingCart = new ShoppingCart();
        BeanUtils.copyProperties(shoppingCartDTO,shoppingCart);
        shoppingCart.setUserId(BaseContext.getCurrentId());

        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        shoppingCart = list.get(0);
        Integer number = shoppingCart.getNumber();
        // 如果数量大于1 ，则更新数量
        if (number>1) {
            shoppingCart.setNumber(number-1);
            shoppingCartMapper.editOneById(shoppingCart);
        }else{// 数量为1， 删除该数据
            ShoppingCart delCart = new ShoppingCart();
            delCart.setId(shoppingCart.getId());
            shoppingCartMapper.delShoppingCart(delCart);
        }
    }

    /**
     * 查看购物车
     *
     * @return
     */
    public List<ShoppingCart> getShoppingCarts() {
        ShoppingCart shoppingCart = new ShoppingCart();
        shoppingCart.setUserId(BaseContext.getCurrentId());
        List<ShoppingCart> list = shoppingCartMapper.list(shoppingCart);
        return list;
    }
}
